<?php


namespace app\controller\admin;


use app\models\user\ProgramInfoModel;
use app\models\user\ProgramProcessModel;
use app\models\user\ProgramProductModel;
use app\services\user\ProgramService;
use app\services\user\UserRecordService;
use think\facade\Db;

class ProgramController extends BaseAdminController
{

    function setSearch()
    {
        return [
            '=' => ['a.product', 'b.process', 'a.start_time', 'a.end_time', 'a.remark_time'],
            'like' => ['a.program_sn','b.user_name','a.program_name'],
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    public function getList()
    {
        try {
            $baseWhere = $this->getBaseWhere('product_ids');
            $where = $this->searchWhere;
            $where = array_merge($baseWhere, $where);
            $orderBy = $this->orderBy;
            $sort = $this->sort;

            $list = ProgramProductModel::alias('a')
                ->field('a.*,b.program_sn,b.program_name,b.process,b.user_name')
                ->leftJoin('program_info b', 'a.program_sn = b.program_sn')
                ->where($where)
                ->order($orderBy, $sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                ProgramProcessModel::where('level', $v['process'])->value('name');
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        Db::startTrans();
        try {
            $data = $this->request->param();
            $info = ProgramProductModel::find($data['id']);
            if (!$info) {
                throw new \Exception('信息不存在');
            }
            $info->save($data);

            $programInfo = ProgramInfoModel::where('program_sn', $data['program_sn'])->find();
            $programInfoArr = $programInfo->toArray();
            $programInfo->save($data);

            UserRecordService::createRecord($data['user_id'], $this->request->userId, $programInfoArr, $data);

            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        Db::startTrans();
        try {
            $id = $this->request->param('id');
            $info = ProgramProductModel::find($id);
            if (!$info) {
                throw new \Exception('信息不存在');
            }
            $info->delete();
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function saveUserRecord()
    {
        Db::startTrans();
        try {
            $id = $this->request->param('id');
            $info = Db::name('program_product')->where('id', $id)->find();
            if (!$info) {
                $data = [
                    'user_id' => $id,
                ];
                $id = Db::name('program_product')->insertGetId($data);
            }
            $data = $this->request->param('data');
            Db::name('program_record')->insert([
                'program_id' => $id,
                'write_uid' => $this->request->userId,
                'context' => $data,
                'create_time' => time()
            ]);
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function getRecordList()
    {
        try {
            $id = $this->request->param('id');
            $where = [];
            $where[] = ['program_id', '=', $id];
            $list = Db::name('program_record')
                ->where($where)
                ->select()
                ->toArray();
            foreach ($list as $k => $v) {
                $list[$k]['write_user_name'] = Db::name('admin_user')->where('id', $v['write_uid'])->value('username');
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function getProcessList()
    {
        try {
            $list = ProgramService::getProcessList();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }


}